<?php

namespace app\modules\admin\controllers;

use Yii;
use app\modules\admin\models\Paymentmember;
use app\modules\admin\models\PaymentmemberSearch;
use app\modules\admin\components\BaseAdminController;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\components\Wechat;

/**
 * PaymentmemberController implements the CRUD actions for Paymentmember model.
 */
class PaymentmemberController extends BaseAdminController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Paymentmember models.
     * @return mixed
     */
    public function actionIndex()
    {
        // $data = ['rule_id'=>444919160]; 
        // $re = Wechat::paygiftcard_delete($data);
        // echo "<pre>";
        // var_dump($re);die;
        // var_dump( Yii::$app->session->get('identifier'));die;
        $searchModel = new PaymentmemberSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        // $data = [
        //     'type' =>'RULE_TYPE_PAY_MEMBER_CARD',
        //     'effective' =>true,
        //     'offset' => 0,
        //     'count' =>2,
        // ];
        // $re = Wechat::paygiftcard_batchget($data);
        // echo "<pre>";
        // var_dump($re);die;
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Paymentmember model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Paymentmember model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Paymentmember();
        if ($model->load(Yii::$app->request->post())) {
            $mchid_list = $model->mchid_list;
            $card_id = $model->card_id;
            $least_cost = $model->least_cost;
            $max_cost = $model->max_cost;
            $jump_url = $model->jump_url;
            $begin_time = strtotime(Yii::$app->request->post('begin_time'));
            $end_time = strtotime(Yii::$app->request->post('end_time'));
            $model->identifier_id = Yii::$app->session->get('identifier');
            // var_dump( Yii::$app->session->get('identifier'));die;
            //组建数据
            $data = [
                'rule_info' => [
                    'type' => 'RULE_TYPE_PAY_MEMBER_CARD',
                    'base_info' => [
                        'mchid_list' => [
                            0 => $mchid_list
                        ],
                        'begin_time' => $begin_time,
                        'end_time'=>$end_time
                    ],
                    'member_rule' => [
                        'card_id' => $card_id,
                        'least_cost' => $least_cost,
                        'max_cost' => $max_cost,
                        'jump_url' => $jump_url
                    ]
                ]
            ];
            $re = Wechat::paygiftcard_create($data);
            if ($re['errmsg'] == 'ok' && !empty($re['succ_mchid_list'])) {
                $model->end_time = $end_time;
                $model->begin_time = $begin_time;
                $model->rule_id = $re['rule_id'];
            } else {
                $erro ='';
               $emsg = array_column($re['fail_mchid_list'],'errmsg');
                foreach ($emsg as $v) {
                    $erro .='&&'.$v;
                }
                echo $erro;die;
            }
            if ($model->save(false)) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing Paymentmember model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }
        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing Paymentmember model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $rule_id = $model->rule_id;
        $data = ['rule_id'=>$rule_id]; 
        $re = Wechat::paygiftcard_delete($data);
        if ($re['errcode'] == 0) {
            $model->delete();
            return $this->redirect(['index']);
        } else {
            echo $re['errmsg'];die;
        }
        
    }

    /**
     * Finds the Paymentmember model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return Paymentmember the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Paymentmember::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
